#include "BoundingBox.h"


const size_t VERTEX_COUNT = 24;
const size_t INDEX_COUNT = 36;
const size_t PRIM_COUNT = INDEX_COUNT / 3;

/*-----------------------------------------------------------------------------------------------*/
BoundingBox::BoundingBox( const Vector3 max, const Vector3 min , DWORD color ): m_visible(true), m_pBowndingVertices(NULL)
{
   m_max = max;
   m_min = min;

   m_pVertices = new ColorVertex[VERTEX_COUNT];

   updateBox(max, min, color);
   
   unsigned short indices[] =
   {
      0, 1, 2,    // side 1
      2, 1, 3,
      4, 5, 6,    // side 2
      6, 5, 7,
      8, 9, 10,   // side 3
      10, 9, 11,
      12, 13, 14, // side 4
      14, 13, 15,
      16, 17, 18, // side 5
      18, 17, 19,
      20, 21, 22, // side 6
      22, 21, 23,
   };

   m_pIndices = new UINT[INDEX_COUNT];

   for (int i = 0; i < INDEX_COUNT; i++)
   {
      m_pIndices[i] = indices[i];
   }
}
/*-----------------------------------------------------------------------------------------------*/
Vector3* BoundingBox::calculateBowndingVertices()
{
   const size_t NUM_BOUNDING_VERTICES = 8;

   if (!m_pBowndingVertices)
   {
      m_pBowndingVertices = new Vector3[NUM_BOUNDING_VERTICES];   
   }

   //8 verticex box with every posible convination
   m_pBowndingVertices[0].x = m_max.x;
   m_pBowndingVertices[0].y = m_max.y;
   m_pBowndingVertices[0].z = m_max.z;

   m_pBowndingVertices[1].x = m_max.x;
   m_pBowndingVertices[1].y = m_max.y;
   m_pBowndingVertices[1].z = m_min.z;

   m_pBowndingVertices[2].x = m_max.x;
   m_pBowndingVertices[2].y = m_min.y;
   m_pBowndingVertices[2].z = m_min.z;

   m_pBowndingVertices[3].x = m_min.x;
   m_pBowndingVertices[3].y = m_min.y;
   m_pBowndingVertices[3].z = m_min.z;

   m_pBowndingVertices[4].x = m_min.x;
   m_pBowndingVertices[4].y = m_min.y;
   m_pBowndingVertices[4].z = m_max.z;

   m_pBowndingVertices[5].x = m_min.x;
   m_pBowndingVertices[5].y = m_max.y;
   m_pBowndingVertices[5].z = m_max.z;

   m_pBowndingVertices[6].x = m_max.x;
   m_pBowndingVertices[6].y = m_min.y;
   m_pBowndingVertices[6].z = m_max.z;

   m_pBowndingVertices[7].x = m_min.x;
   m_pBowndingVertices[7].y = m_max.y;
   m_pBowndingVertices[7].z = m_min.z;

  return m_pBowndingVertices;
}
/*-----------------------------------------------------------------------------------------------*/
void BoundingBox::draw( Renderer* renderer )
{
   if ( isVisible() )
   {
      renderer->unbindTexture();
      renderer->loadIdentity();
      renderer->setWireframeEnable(true);
      renderer->setLightsEnable(false);
      renderer->DrawElements(VERTEX_COUNT, m_pVertices, INDEX_COUNT, m_pIndices, PRIM_COUNT, TRIANGLE_LIST);
      renderer->setWireframeEnable(false);
      renderer->setLightsEnable(true);
   }
}
/*-----------------------------------------------------------------------------------------------*/
BoundingBox::~BoundingBox()
{
   delete m_pVertices;
   delete m_pIndices;
   delete m_pBowndingVertices;
}
/*-----------------------------------------------------------------------------------------------*/
void BoundingBox::updateBox( const Vector3 max, const Vector3 min , DWORD color )
{
   m_max = max;
   m_min = min;

   m_pVertices[ 0].setVertex( min.x, min.y, max.z, color );
   m_pVertices[ 1].setVertex( max.x, min.y, max.z, color );
   m_pVertices[ 2].setVertex( min.x, max.y, max.z, color );
   m_pVertices[ 3].setVertex( max.x, max.y, max.z, color );

   m_pVertices[ 4].setVertex( min.x, min.y, min.z, color );
   m_pVertices[ 5].setVertex( min.x, max.y, min.z, color );
   m_pVertices[ 6].setVertex( max.x, min.y, min.z, color );
   m_pVertices[ 7].setVertex( max.x, max.y, min.z, color );

   m_pVertices[ 8].setVertex( min.x, max.y, min.z, color );
   m_pVertices[ 9].setVertex( min.x, max.y, max.z, color );
   m_pVertices[10].setVertex( max.x, max.y, min.z, color );
   m_pVertices[11].setVertex( max.x, max.y, max.z, color );

   m_pVertices[12].setVertex( min.x, min.y, min.z, color );
   m_pVertices[13].setVertex( max.x, min.y, min.z, color );
   m_pVertices[14].setVertex( min.x, min.y, max.z, color );
   m_pVertices[15].setVertex( max.x, min.y, max.z, color );

   m_pVertices[16].setVertex( max.x, min.y, min.z, color );
   m_pVertices[17].setVertex( max.x, max.y, min.z, color );
   m_pVertices[18].setVertex( max.x, min.y, max.z, color );
   m_pVertices[19].setVertex( max.x, max.y, max.z, color );

   m_pVertices[20].setVertex( min.x, min.y, min.z, color );
   m_pVertices[21].setVertex( min.x, min.y, max.z, color );
   m_pVertices[22].setVertex( min.x, max.y, min.z, color );
   m_pVertices[23].setVertex( min.x, max.y, max.z, color );
}
